<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/22
 * Time: 15:43
 */
require_once  Root.'/vendor/autoload.php';
class BaseController extends Controller
{

    private $isLogin = false;
    private $user_name = "";
    public function __construct()
    {
        $this->checkIsLogin();
        $this->init();
    }

    public function checkIsLogin()
    {
        $token = $_SESSION["token"];
        if(empty($token)){
            $this->isLogin = false;
        }else {
            $info = unserialize($token);
            if ($info["user_name"]) {
                $this->isLogin = true;
                $this->user_name = $info["user_name"];
                $info["time"] = time();
                $_SESSION["token"] = serialize($info);
            }
        }
        $this->out["isLogin"] = $this->isLogin;
        $this->out["user_name"] = $this->user_name;
    }

    public function init()
    {
        $roomList = array(
            1 => "公共聊天室",
            2 => "php交流专用房间",
            3 => "java交流专用房间",
            4 => "交友聊天室",
            5 => "4399聊天室",
        );
        $room_id = empty($_GET["room_id"]) ? 1 : $_GET["room_id"];
        $room_name = empty($roomList[$room_id]) ? "不存在该房间" : $roomList[$room_id];
        //获取用户头像id，用第一次登陆的时候client_id做唯一标识http://lorempixel.com/38/38/?client_id
        $mod = new ModRecordLog();
        $userInfo = $mod->getUserImageId($this->user_name);
        if(empty($userInfo[0]["image_id"])){
            $userInfo[0]["image_id"] = "1";//随便给一个固定的初始值，一般不会为空
        }
        $this->out["user_image_id"] = $userInfo[0]["image_id"];
        $this->out["room_id"] = $room_id;
        $this->out["room_name"] = $room_name;
        $this->out["roomList"] = $roomList;
        $this->out["allUserImageId"] = urlencode(json_encode($mod->getAllUserImageId()));
    }
}